package com.asiainfo.dacp.datastash.worker.executor.adapter.datax.model.plugin;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.google.common.collect.Lists;

import lombok.Data;
import lombok.NonNull;

@Data 
public class RdbmsReader implements Serializable{   
	private static final long serialVersionUID = -9117051721554606891L; 
	
	private String username;
	private String password; 
	private List<String> column; 
	 
	private List<Connection> connection;
	private String splitPk;
	/**
	 * 该配置项定义了插件和数据库服务器端每次批量数据获取条数，该值决定了DataX和服务器端的网络交互次数，能够较大的提升数据抽取性能。
	 */
	private int fetchSize = 1024;
	
	private List<String> session;
	
	/**
	 * 根据指定的column、table、where条件拼接SQL
	 */
	private String where; 
	
	public RdbmsReader(){}
	public RdbmsReader(RdbmsReader source){
		BeanUtils.copyProperties(source, this);
	}
	
	public void initDataSource( @NonNull String username, @NonNull String password, @NonNull String jdbcUrl){
		this.setPassword(password);
		this.setUsername(username);
		
		if(this.connection == null){
			this.setConnection(Lists.newArrayList(new Connection(jdbcUrl)));
		} else{
			connection.get(0).setJdbcUrl(Lists.newArrayList(jdbcUrl));
		}
		//this.connection = new Connection(Lists.newArrayList(jdbcUrl));
	}
	
	public void setTable(List<String> table){
		connection.get(0).setTable(table);
	}
	public void setQuerySql(List<String> querySql){
		connection.get(0).setQuerySql(querySql);
	}
	
	@Data 
	private class Connection {
		private List<String> table;
		private List<String> jdbcUrl; 
		/**
		 * 当配置querySql时，忽略table、column、where条件的配置。
		 */
		private List<String> querySql;
		
		public Connection(String jdbcUrl){
			this.setJdbcUrl(Lists.newArrayList(jdbcUrl));
		}
	} 
}
